package com.shenmazong.servereduuser.controller;

import com.shenmazong.servereduuser.entity.TbAdmin;
import com.shenmazong.servereduuser.entity.TbStaff;
import com.shenmazong.servereduuser.entity.TbStudent;
import com.shenmazong.servereduuser.service.IndexService;
import com.shenmazong.utils.ConstUtils;
import com.shenmazong.utils.JwtUtils;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.AccessTokenVo;
import com.shenmazong.vo.TokenInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author 军哥
 * @version 1.0
 * @description: 认证相关接口
 * @date 2021/9/16 14:32
 */

@Api(tags = {"认证API接口"})
@RestController
@Slf4j
@RequestMapping(value = "/auth")
public class IndexController {
    @Autowired
    private IndexService indexService;

    @PostMapping(value = "/publicKey")
    public ResultResponse publicKey() {

        HashMap<String, Object> data = new HashMap<>();
        data.put("mockServer", false);
        data.put("publicKey", "helloworld");

        return ResultResponse.SUCCESS(data);
    }

    @PostMapping(value = "/login")
    public ResultResponse login() {

        HashMap<String, Object> data = new HashMap<>();
        data.put("accessToken", "accessToken");

        return ResultResponse.SUCCESS(data);
    }

    @ApiOperation(value="获取当前用户信息", notes="获取当前用户信息", produces="application/json")
    @PostMapping(value = "/userInfo")
    public ResultResponse userInfo(@RequestBody AccessTokenVo tokenVo) {
        System.out.println(tokenVo.getAccessToken());
        //1.解析token
        TokenInfo tokenByString = JwtUtils.getTokenByString(tokenVo.getAccessToken());
        Long loginId = tokenByString.getLoginId();
        String role = tokenByString.getLoginRole();
        HashMap<String, Object> hashMap = null;
        if(role.equals(ConstUtils.MANAGER_ROLE)){
            hashMap=indexService.selectManager(loginId);
        }

        if(role.equals(ConstUtils.TEACHER_ROLE)){
            hashMap= indexService.selectStaff(loginId);
        }

        if(role.equals(ConstUtils.STUDENT_ROLE)){
            hashMap=indexService.selectStudent(loginId);
        }

        return ResultResponse.SUCCESS(hashMap);
    }

    @ApiOperation(value="退出登录", notes="退出登录", produces="application/json")
    @PostMapping(value = "/logout")
    public ResultResponse logout(@RequestBody AccessTokenVo tokenVo) {

        log.info("logout:"+tokenVo);

        return ResultResponse.SUCCESS();
    }
}
